/*
 * Copyright (c) 2020-2021 imlzw@vip.qq.com jweb.cc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cc.jweb.adai.web.system.generator.utils;

import com.jfinal.core.NotAction;

/**
 * 版本工具
 */
public class VersionUtils {

    /**
     * 比较两个版本大小
     *
     * @param version1
     * @param version2
     * @return int 0：v1 eq v2, >0:v1 gt v2, <0: v1 lt v2
      */
    public static int compareVersion(String version1, String version2) {
        String[] version1s = version1.split("\\.");
        String[] version2s = version2.split("\\.");
        boolean exchange = false;
        if (version2s.length > version1s.length) {
            String [] temp = version1s;
            version1s = version2s;
            version2s = temp;
            exchange = true;
        }
        for (int i = 0; i < version1s.length; i++) {
            String num1 = version1s[i];
            if (version2s.length > i) {
                String num2 = version2s[i];
                if (num1.compareTo(num2) == 0) {
                    continue;
                } else {
                    return exchange ? num2.compareTo(num1) : num1.compareTo(num2);
                }
            } else {
                return exchange ? -1 : 1;
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println(compareVersion("1.0.3.1", "1.0.3.1") == 0);
        System.out.println(compareVersion("1.0.3.1", "1.0.3.2") == -1);
        System.out.println(compareVersion("1.0.3.2", "1.0.3.1") == 1);
        System.out.println(compareVersion("1.0.3.2", "1.0.3") == 1);
        System.out.println(compareVersion("1.0.3", "1.0.3") == 0);
        System.out.println(compareVersion("1.0.3", "1.0.3.1") == -1);
        System.out.println(compareVersion("1.0.2.1", "1.0.3.1") == -1);
        System.out.println(compareVersion("1.0.4.1", "1.0.3.1") == 1);
    }
}
